
classdef IS95ReverseLinkPowerControlStrategyImpl < cdmapc.IPowerControlStrategy
    
    properties (GetAccess = private, SetAccess = private)
        accessChannelAcquired = 0;
        
        openLoopStrategy
        closedLoopStrategy
    end
    
    events
        reverseLinkPowerControlPerformed
    end % events
    
    methods (Access = public)
        
        %
        % Constructor
        %
        function [iS95ReverseLinkPowerControlStrategyImpl] = IS95ReverseLinkPowerControlStrategyImpl(openLoopStrategy, closedLoopStrategy)
            if(nargin == 0)
                error('Debe proporcionarse la estrategia en lazo abierto para el reverse link');
            elseif(nargin == 1)
                error('Debe proporcionarse la estrategia en lazo cerrado para el reverse link');
            elseif(~isa(openLoopStrategy, 'cdmapc.IPowerControlStrategy'))
                error('El objeto openLoopStrategy debe ser de tipo IPowerControlStrategy');
            elseif(~isa(closedLoopStrategy, 'cdmapc.IPowerControlStrategy'))
                error('El objeto closedLoopStrategy debe ser de tipo IPowerControlStrategy');
            end % if
            iS95ReverseLinkPowerControlStrategyImpl.openLoopStrategy = openLoopStrategy;
            iS95ReverseLinkPowerControlStrategyImpl.closedLoopStrategy = closedLoopStrategy;
            iS95ReverseLinkPowerControlStrategyImpl.addListeners();
        end % function
        
        %
        % Destructor
        %
        function [] = delete(this)
            this.accessChannelAcquired = 0;
        end % function
        
        function [] = applyStrategy(this)
            if(this.accessChannelAcquired == 0)
                this.openLoopStrategy.applyStrategy();
            else
                this.closedLoopStrategy.applyStrategy();
            end % if
        end % function
        %
        %     end % methods
        %
        %     methods (Access = private)
        
        function [] = addListeners(this)
            addlistener(this.openLoopStrategy, 'accessChannelAcquired', @this.handleAccessChannelAcquired);
            addlistener(this.openLoopStrategy, 'reverseLinkOpenLoopPowerControlPerformed', @this.handlePowerControlPerformed);
            addlistener(this.openLoopStrategy, 'reverseLinkClosedLoopPowerControlPerformed', @this.handlePowerControlPerformed);
        end % function
        
        function [] = handleAccessChannelAcquired(this, ~, ~)
            this.accessChannelAcquired = 1;
        end % function
        
        function [] = handlePowerControlPerformed(this, src, evnt)
            notify(this, 'reverseLinkPowerControlPerformed', evnt);
        end % function
        
    end % methods
    
end % classdef
